//
//  HonorTableView.m
//  HomePage
//
//  Created by HanHaiDi on 13-8-21.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "HonorTableView.h"
#import "HonorTableCell.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"
#import "../../ResourceLoader/ResourceLoader/JSONKit.h"

#define AwardingUnits_Row_Capacity 24
#define FontHeight 20
#define TopSpace 20

@implementation HonorTableView
@synthesize currentDatas;

- (id)initWithFrame:(CGRect)frame templeteFilePath:(NSString*)filePath
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStyleGrouped];
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [tableView setShowsHorizontalScrollIndicator:false];
        [tableView setShowsVerticalScrollIndicator:false];
        [tableView setDelegate:self];
        [tableView setDataSource:self];
        [self addSubview:tableView];
        [tableView release];
        [tableView setBackgroundView:[[[UIView alloc] init] autorelease]];
        
        ResourceLoader *loader = [ResourceLoader getInstance];
        
        self.currentDatas = [loader getBrandHonor:filePath];
        
        
    }
    return self;
}

#pragma mark - TableView Delegate & DataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary * dic = [currentDatas objectAtIndex:section];
    NSArray * arrContent = [dic objectForKey:@"Content"];
    
    return arrContent == nil ?0:[arrContent count];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return currentDatas == nil?0:[currentDatas count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * dic = [currentDatas objectAtIndex:indexPath.section];
    NSArray * arrContent = [dic objectForKey:@"Content"];
    NSDictionary * dicContent = [arrContent objectAtIndex:indexPath.row];
    NSString * awardingUnits = [dicContent objectForKey:@"AwardingUnits"];
    NSInteger rowCount = (awardingUnits.length / AwardingUnits_Row_Capacity) +1;
    return awardingUnits.length< AwardingUnits_Row_Capacity ? 40: (rowCount*FontHeight)+TopSpace;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 450, 40)];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextColor:[UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0]];
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label.textAlignment = NSTextAlignmentLeft;
    
    NSDictionary * dic = [currentDatas objectAtIndex:section];
    label.text = [dic objectForKey:@"Title"];
    return [label autorelease];
}

- (UITableViewCell*)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HonorTableCell* cell = [tView dequeueReusableCellWithIdentifier:@"HonorTableCell"];
    if(cell == nil)
    {
        cell = [[[HonorTableCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:@"HonorTableCell"] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    NSDictionary * dic = [currentDatas objectAtIndex:indexPath.section];
    NSArray * arrContent = [dic objectForKey:@"Content"];
    [cell loadData:[arrContent objectAtIndex:indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


-(void)dealloc
{
     NSLog(@"currentDatas ref=%d",[currentDatas retainCount]);
    self.currentDatas = nil;
    [super dealloc];
}



@end
